package com.th.therp.controller;

import com.th.therp.exception.BaseException;
import com.th.therp.pojo.emtity.Store;
import com.th.therp.pojo.result.Result;
import com.th.therp.service.StoreService;
import jakarta.validation.Valid;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库请求控制器
 */
@Log4j2
@RestController
@RequestMapping("/store")
public class StoreController {

    @Autowired
    private StoreService storeService;

    //查询全部仓库
    @GetMapping("/page")
    public Result<List<Store>> list(){
        log.info("查询全部仓库");
        List<Store> storeList = storeService.list();
        return Result.success(storeList);
    }

    //新增仓库
    @PostMapping
    public Result add(@Valid @RequestBody Store store, BindingResult bindingResult){
        log.info("新增仓库:{}",store);
        if (bindingResult.hasErrors()){
            throw new BaseException("格式不正确");
        }
        storeService.add(store);
        return Result.success();
    }

    //修改仓库
    @PutMapping
    public Result update(@Valid @RequestBody Store store,BindingResult bindingResult){
        log.info("修改仓库:{}",store);
        if (bindingResult.hasErrors()){
            throw new BaseException("格式不正确");
        }
        storeService.update(store);
        return Result.success();
    }

    //删除仓库
    @DeleteMapping()
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除仓库:{}",ids);
        storeService.delete(ids);
        return Result.success();
    }
}